package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.AdviceService;
import com.yuwubao.uav.tool.util.ArticleSearchService;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 *通知管理
 */
@RestController
@RequestMapping("/bk")
public class AdviceController {

    private static final Logger logger = LogManager.getLogger(AdviceController.class);

    @Resource
    private AdviceService adviceService;

    @Resource
    private ArticleSearchService articleSearchService;

    @LoginRequired
    @PostMapping("/addAdvice")
    @ApiOperation(value = "新增通知")
    public JsonResult addAdvice(
            @ApiParam(value = "标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "附件名称，多个用逗号分隔,和url一一对应") @RequestParam(value = "attachUrls",required = false) String attachUrls,
            @ApiParam(value = "附件URL，多个用逗号分隔,和名称一一对应") @RequestParam(value = "attachs",required = false) String attachs
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("title", title);
            pageData.put("author", author);
            pageData.put("origin", origin);
            pageData.put("content", content);
            pageData.put("attachUrls", attachUrls);
            pageData.put("attachs", attachs);

            adviceService.addAdvice(pageData);

            try {
                pageData.put("tableName","t_advice");
                articleSearchService.createDoc(pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateAdvice")
    @ApiOperation(value = "更新通知")
    public JsonResult updateAdvice(
            @ApiParam(value = "ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "附件名称，多个用逗号分隔,和url一一对应") @RequestParam(value = "attachUrls",required = false) String attachUrls,
            @ApiParam(value = "附件URL，多个用逗号分隔,和名称一一对应") @RequestParam(value = "attachs",required = false) String attachs
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("title", title);
            pageData.put("author", author);
            pageData.put("origin", origin);
            pageData.put("content", content);
            pageData.put("attachs", attachs);
            pageData.put("attachUrls", attachUrls);

            adviceService.updateAdvice(pageData);

            try {
                pageData.put("tableName","t_advice");
                articleSearchService.update(id,pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteAdvice")
    @ApiOperation(value = "删除通知")
    public JsonResult deleteAdvice(
            @ApiParam(value = "通知ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            adviceService.deleteAdvice(pageData);

            try {
                if (StringUtil.isNotNvlOrBlank(ids)) {
                    pageData.put("tableName","t_advice");
                    for (String id : ids.split(",")) {
                        pageData.put("id",id);
                        articleSearchService.delete(pageData);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableAdvice")
    @ApiOperation(value = "启用或者禁用通知通知")
    public JsonResult disableOrEnableAdvice(
            @ApiParam(value = "通知ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            adviceService.disableOrEnableAdvice(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listAdvice")
    @ApiOperation(value = "获取通知列表")
    public JsonResult listAdvice(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(adviceService.listAdvice(pageData));
            result.setResultNum(adviceService.listAdviceCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listAdviceEnrollUser")
    @ApiOperation(value = "获取通知报名人员列表")
    public JsonResult listAdviceEnrollUser(
            @ApiParam(value = "通知ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(adviceService.listAdviceEnrollUser(pageData));
            result.setResultNum(adviceService.listAdviceEnrollUserCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


}
